﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using Newtonsoft.Json;

namespace SweetWeather
{
    public class IPManager
    {
        /// <summary>
        /// 得到本地Ipv4数组
        /// </summary>
        /// <returns></returns>
        public static string[] GetLocalIpv4()
        {
            //事先不知道ip的个数，数组长度未知，因此-0用StringCollection储存
            IPAddress[] localIPs;
            localIPs = Dns.GetHostAddresses(Dns.GetHostName());
            StringCollection IpCollection = new StringCollection();
            foreach (IPAddress ip in localIPs)
            {
                //根据AddressFamily判断是否为ipv4,如果是InterNetWork则为ipv6
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                    IpCollection.Add(ip.ToString());
            }
            string[] IpArray = new string[IpCollection.Count];
            IpCollection.CopyTo(IpArray, 0);
            return IpArray;
        }

        /// <summary>
        /// 通过调用PCOnline返回IP对应的地区
        /// 调用搜狐Ip失败时调用
        /// </summary>
        /// <returns></returns>
        public static string[] GetNetWorkIpAddressByPCOnline(string strUrl)
        {
            string all = GetWebResponseString(strUrl,Encoding.Default);
            int startIndex = all.LastIndexOf("{");
            int endIndex = all.LastIndexOf("}") - 2;
            string tempip = all.Substring(startIndex, endIndex - startIndex);
            var result = JsonConvert.DeserializeObject<PCOnlineAddress>(tempip);
            if (result != null)
                return new string[2] { result.pro, result.city };
            else
                return null;
        }

        /// <summary>
        /// 读取网站返回数据
        /// </summary>
        /// <param name="strUrl">网站链接</param>
        /// <returns></returns>
        public static string GetWebResponseString(string strUrl, Encoding encode)
        {
            Uri uri = new Uri(strUrl);
            WebRequest webreq = WebRequest.Create(uri);
            Stream s = webreq.GetResponse().GetResponseStream();
            StreamReader sr = new StreamReader(s,encode);
            string all = sr.ReadToEnd();         //读取网站返回的数据
            return all;
        }

        /// <summary>
        /// 通过调用新浪返回IP地址和地区
        /// 调用PCOnline异常时调用
        /// </summary>
        /// <returns></returns>
        public static string[] GetNetWorkIpAddressBySina(string strUrl)
        {
            string all = GetWebResponseString(strUrl, Encoding.Default);
            int startIndex = all.IndexOf("{");
            int endIndex = all.IndexOf("}") + 1;
            string tempip = all.Substring(startIndex, endIndex - startIndex);
            var result = JsonConvert.DeserializeObject<SinaAddress>(tempip);
            if (result != null)
                return new string[2] { result.province, result.city };
            else
                return null;
        }
    }

    /// <summary>
    /// PCOnline返回信息
    /// </summary>
    public class PCOnlineAddress
    {
        //"ip":"122.192.188.95","pro":"江苏省","city":"徐州市","region":"","addr":"江苏省徐州市 联通","regionNames":""
        public string ip { get; set; }
        public string pro { get; set; }
        public string city { get; set; }
        public string region { get; set; }
        public string addr { get; set; }
        public string regionNames { get; set; }
    }

    /// <summary>
    /// Sina返回信息
    /// </summary>
    public class SinaAddress
    {
        //{"ret":1,"start":"122.192.152.0","end":"122.192.231.255","country":"\u4e2d\u56fd",
        //"province":"\u6c5f\u82cf","city":"\u5f90\u5dde","district":"","isp":"\u8054\u901a","type":"","desc":""}
        public string ret { get; set; }
        public string start { get; set; }
        public string end { get; set; }
        public string country { get; set; }
        public string province { get; set; }
        public string city { get; set; }
        public string district { get; set; }
        public string isp { get; set; }
        public string type { get; set; }
        public string desc { get; set; }
    }
}
